/**********************************************************************
 * JSC(JavaScript Component): Coder
 * Copyright (c) 2007 lenel(lenel@yahoo.cn)
 * JSC is freely distributable under the terms of an MIT-style license.
 * $Date: 2007-09
 **********************************************************************/
coder.prototype = new Jsc;
function coder(o){
	this.loaded = false;
	this.doc = o;
	this.c_type = "coder";
	this.cid = "o"+o.id;
	this.bid = o.id;
	o.jsc = this;
	this.options = new JscSet();
	this.codeList;
	this.popup;
	this.curOption = null;
	this.init = function(){
		this.initBase();
		this.show();
		this.addEventListener();
		this.buildCodeList();
		this.setOptions();		
		this.popup = new JscPopupClk(this.codeList,null,"");
		_jsc.util.addEvent(this.popup.body,'click',this.listOnClick);		
	};
		
	this.show = function(){
		var s = '<table cellspacing=0 cellpadding=0 ><tbody><tr>';
		s += '<td id="'+this.bid+'_td" style="font-size:12px;border:1px #A4B97F solid;padding-left:2px">';
		s += '<input id="'+this.bid+'_ipt" readonly style="border:0px"/>';
		s += '<span id="'+this.bid+'_icon" flag="dropIcon" style="cursor:pointer;background:url(\'../img/menu.gif\') no-repeat 0px 1px;font-size:17px;">&nbsp;&nbsp;</span>	';
		s += '</td></tr></tbody></table>';
		this.doc.innerHTML = s;
	};
	
	this.addEventListener = function(){
		_jsc.util.addEvent(this.doc,'click',this.jscOnClick);
	};
	
	this.jscOnClick = function(){		
		var oEvent = window.event?window.event:arguments[0];
		var oTarget = oEvent.target?oEvent.target:oEvent.srcElement;
		if(oTarget.getAttribute("flag")&&oTarget.getAttribute("flag")=="dropIcon"){
			var curJsc = _jsc.evt.gJsc(oTarget).jsc;
			curJsc.buildOptionsDoc();
			_jsc.popup.clk.show(oEvent,curJsc.codeList.id,oTarget.parentNode,0,0);
		}
	};
	
	this.listOnClick = function(){
		var oEvent = window.event?window.event:arguments[0];
		var oTarget = oEvent.target?oEvent.target:oEvent.srcElement;
		if(oTarget.getAttribute("nodeflag")&&oTarget.getAttribute("nodeflag")=="jscOption"){
			var d = oTarget.parentNode.id
			var s = d.substr(0,d.length-8);
			var curJsc = $i(s).jsc;
			var curOption = curJsc.options.arr[curJsc.options.idx[oTarget.getAttribute("value")]][1];
			curJsc.setSelectValue(curOption);			
		}		
	};
	
	this.setOptions = function(){
		var opWidth = $i(this.bid+"_td").offsetWidth - 2;
		eval("var arr = "+this.c_options);
		for(var i=0;i<arr.length;i++){
			this.options.add(arr[i][0],new JscOption(arr[i][0],arr[i][1],opWidth));
		}
		this.setSelectValue(this.options.getFirst(),true);
	};
	
	this.buildOptionsDoc = function(){
		this.codeList.innerHTML = "";
		for(var i=0;i<this.options.arr.length;i++){
			if(this.options.arr[i][1]){
				this.codeList.innerHTML+=this.options.arr[i][1].doc;
				if(this.options.arr[i][1].checked){}
			}
		}
	};
	
	this.setSelectValue = function(jscOption,init){
		this.curOption = jscOption;
		$i(this.bid+"_ipt").value = jscOption.text;
		if(!init){
			var evt = new Object();
			evt.curJsc = this;
			evt.curOption = jscOption;
			_jsc.evt.fire(this,"change",evt);
		}
	};
	
	this.buildCodeList = function(){
		var oDiv = document.createElement("div");
		oDiv.id = this.bid+"CodeList";
		oDiv.style.position = "absolute";
		oDiv.style.display = "none";		
		oDiv.style.border = "1px solid #A4B97F";
		oDiv.style.borderTop = "0px";
		oDiv.style.fontSize = "12px";
		oDiv.innerHTML="&nbsp;";
		this.codeList = document.body.appendChild(oDiv);
	};
	this.init(); 
}


function JscOption(sValue,sText,width){
	this.value = sValue;
	this.text = sText;
	this.checked = false;
	this.doc="<div nodeflag='jscOption' onmouseover=\"this.style.backgroundColor='#A4B97F'\" onmouseout=\"this.style.backgroundColor='#FFFFFF'\" value=\""+sValue+"\" style=\"overflow:hidden;height:16px;cursor:pointer;font-size:12px;width:"+width+"px\" text=\""+sText+"\" title=\""+sText+"\" >"+sText+"</div>";
}

